package bean;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;

import org.primefaces.model.UploadedFile;

import Model.NhaSanXuat;
import Model.SanPham;
import Util.DAO;
import Util.Util;

@ManagedBean
@SessionScoped
public class BeanSuaSanPham implements Serializable{
	
	private UploadedFile fileUpload;
	private SanPham selectedSanPham;
	private boolean loigia;
	private boolean loihang;
	private boolean loifile;
	
	public boolean isLoifile() {
		return loifile;
	}
	public void setLoifile(boolean loifile) {
		this.loifile = loifile;
	}
	public boolean isLoigia() {
		return loigia;
	}
	public void setLoigia(boolean loigia) {
		this.loigia = loigia;
	}
	public boolean isLoihang() {
		return loihang;
	}
	public void setLoihang(boolean loihang) {
		this.loihang = loihang;
	}
	public UploadedFile getFileUpload() {
		return fileUpload;
	}
	public void setFileUpload(UploadedFile fileUpload) {
		this.fileUpload = fileUpload;
	}	
	public SanPham getSelectedSanPham() {
		FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> paramMap = context.getExternalContext().getRequestParameterMap();
        String ten = paramMap.get("ten");
        if(ten != null){
        try {
			ten = new String(ten.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        selectedSanPham = DAO.kiemtrasanpham(ten);
        }
		return selectedSanPham;
	}
	public void setSelectedSanPham(SanPham selectedSanPham) {
		this.selectedSanPham = selectedSanPham;
	}
	
	public void upload() {
		
        if(fileUpload != null) {
        	String ten = fileUpload.getFileName();
        	if(ten.endsWith(".jpg") || ten.endsWith(".jpeg") || ten.endsWith(".gif") || ten.endsWith(".png")){
            try {
            	loifile = false;
            	selectedSanPham.setHinh(selectedSanPham.getTen()+".jpg");
				copyFile(selectedSanPham.getTen());
				System.out.println("da tao file moi");
			} catch (Exception e) {
				e.printStackTrace();
			}
        	}
        	else{
        		loifile = true;
        	}
        }
    }
    
    private void copyFile(String fileName) {
		try {
			ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        	String folder = ctx.getRealPath("/resources/images/hinhsanpham/");
        	BufferedImage originalImage = ImageIO.read(fileUpload.getInputstream());
			int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
	 
			BufferedImage resizeImageJpg = Util.resizeImage(originalImage, type);
			ImageIO.write(resizeImageJpg, "jpg", new File(folder+"/"+fileName+".jpg")); 

		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
    }
    public String luusanpham(){
    	if(selectedSanPham.getGia().intValue() < 5000000){
			loigia = true;
		}
		else{
			loigia = false;
		}
		if(selectedSanPham.getNhasanxuat() == null
				|| selectedSanPham.getNhasanxuat().getTennhasanxuat() == ""
				){
			loihang = true;
		}
		else{
			loihang = false;
		}
		if(loigia || loihang || loifile){
			return null;
			}
		NhaSanXuat nsx1 = DAO.laynhasanxuat(selectedSanPham.getNhasanxuat().getIdnhasanxuat());
		if(nsx1 == null){
			DAO.luunhasanxuat(selectedSanPham.getNhasanxuat());
		}
		try {
			selectedSanPham.setCpu(new String(selectedSanPham.getCpu().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setRam(new String(selectedSanPham.getRam().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setHdd(new String(selectedSanPham.getHdd().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setManHinh(new String(selectedSanPham.getManHinh().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setDoHoa(new String(selectedSanPham.getDoHoa().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setoDia(new String(selectedSanPham.getoDia().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setWifi(new String(selectedSanPham.getWifi().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setWebcam(new String(selectedSanPham.getWebcam().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setPin(new String(selectedSanPham.getPin().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setHdh(new String(selectedSanPham.getHdh().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setBaoHanh(new String(selectedSanPham.getBaoHanh().getBytes("ISO-8859-1"),"utf-8"));
			selectedSanPham.setKhuyenMai(new String(selectedSanPham.getKhuyenMai().getBytes("ISO-8859-1"),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DAO.capnhat(selectedSanPham);
		return "quanlysanpham?faces-redirect=true&amp&reset=true";
		
		}
    public String xoasanpham(){
		DAO.xoasanpham(selectedSanPham);
		return "quanlysanpham?faces-redirect=true&amp&reset=true";
	}
    public String quaylai(){
    	selectedSanPham = new SanPham();
    	fileUpload = null;
    	loifile = false; loigia = false; loihang = false;
    	return "quanlysanpham";
    }
	
}
